$(document).ready(function() {
		
	$('.jqTransform').jqTransform({
		imgPath : 'images/'
	});
	
		notifierInst = new notifier($("#notifier"));
		
		// Grid to load holidays list
		jQuery("#list3").jqGrid({ url:'ttgetDataServlet', datatype: "json", colNames:['Holiday Date', 'Description'], colModel:[ {name:'invdate',index:'invdate', width:190, resizable: false}, {name:'note',index:'note', width:290, sortable:false, resizable: false} ], rowNum:20, rowList:[10,20,30], pager: '#pager3', sortname: 'invdate', viewrecords: false, sortorder: "desc", caption: "List of Holidays", autowidth: true, loadui: "disable"});
		$('.ui-jqgrid-titlebar-close','.ui-jqgrid').remove();
		
		jQuery("#list3").jqGrid('navGrid','#pager3',{edit:false,add:false,del:false});
		
		// Grid to load users list
		jQuery("#users").jqGrid({ url:'ttgetDataServlet?action=usersList', datatype: "json", colNames:['User Id', 'Login Name', 'First Name', 'Last Name'], colModel:[ {name:'userid',index:'userid', width: 50, resizable: false}, {name:'loginname',index:'loginname', resizable: false}, {name:'fname',index:'fname', resizable: false}, {name:'lname',index:'lname', sortable:false, resizable: false} ], rowNum:30, rowList:[10,20,30], pager: '#pagers', sortname: 'loginname', viewrecords: false, sortorder: "desc", caption: "List of Users", autowidth: true, loadui: "disable"});
		$('.ui-jqgrid-titlebar-close','.ui-jqgrid').remove();
		jQuery("#users").jqGrid('navGrid','#pagers',{edit:false,add:false,del:false});

		// Configuring date pickers
		$("#from, #to, #leavefrom, #leaveto, #reimfrom, #reimto, #holiday, #billDate").datepicker({
			changeMonth : true,
			changeYear : true,
			numberOfMonths : 1
		});
		
		// Configuring time pickers
		$('#fromHrs, #toHrs, #genShiftStart').timepicker({
			hourGrid: 4,
			minuteGrid: 10,
			stepMinute: 5			
		});
		$("#genShiftEnd").attr('disabled', 'true');
		$("#tabs").tabs();

		// Defining auto complete
		$( "#manager" ).autocomplete({
			source: "ttgetDataServlet?action=managerList",
			minLength: 2,
			select: function(event, data) {
				$("#managerId").val(data.item.id);
			}
		});
		
/*		$.post(
			"dbmgmt",
			{action: 'loadTables'},
			function (data) {
				if (data){
			   		$.each(data.tables, function (index) {
			   			$('#tableList').append(new Option(this.value, this.key, false, false));
			   		});
			   		$.each(data.users, function (index) {
			   			$('#userList').append(new Option(this.value, this.key, false, false));
			   		});
				}
				$('.database1').jqTransform({
					imgPath : 'images/'
				});
			}
		);
*/		
		function constructRows(index, obj, parentId) {
			var rows, strtRow = '<tr id="row'+index+'">'
					 + '<td width="5%"><input type="checkbox" class="cb-element" name="check_'+index+'" id="check_'+ index + '" value="'+obj.id+'"'+ obj.disabled+'/></td>' 
					 + '<td id="name'+index+'">'+'<a href="#" id="subReportees">'+obj.name+'</a>'+'</td>'
					 + '<td><select id="selectedMonth'+index+'" name="selectedMonth'+index+'"><option value="-1">Select Month</option><option value="0">January</option><option value="1">February</option><option value="2">March</option>'
				 + '<option value="3">April</option><option value="4">May</option><option value="5">June</option><option value="6">July</option><option value="7">August</option>'
				 + '<option value="8">September</option><option value="9">October</option><option value="10">November</option><option value="11">December</option>'
				 + '</select></td>',
			radioRows = '<td><input type="radio" name="selectRadio'+index+'" value="0" style="width: 110px;"/></td>'
					 + '<td><label class="forradio">Reimbursement</label></td>'
					 + '<td><input type="radio" name="selectRadio'+index+'" value="1" style="width: 110px;"/></td>'
					 + '<td><label class="forradio">Cash &amp; Travel Voucher</label></td>'
					 + '<td><input type="radio" name="selectRadio'+index+'" value="2" style="width: 110px;" checked="true"></td>'
					 + '<td><label class="forradio">Both the Reports</label></td>',
			endRow = '</tr>';
			if (obj.disabled !== "") {
				rows = '<td colspan="6" align="center" style="color:red;">' + obj.message + '</td>';
			}
			else {
				rows = radioRows;
			}
			//$("#myTable tr:last").after('<tr><td><input type="checkbox" class="cb-element" id="check'+ index + '"/></td><td>'+this+'</td><td><select id="select'+index+'" style="width: 110px; height: auto;"><option>Select activity</option><option>Reimbursement</option><option>Cash &amp; Travel Voucher</option></select></td><td>Action</td></tr>');
			$("#"+parentId).append(strtRow + rows + endRow);
		};
		
		$.post(
			"ttgetDataServlet",
			{action: 'getReportees'},
			function(data) {
				if (data.length >0){
			   		$.each(data, function (index) {
			   			constructRows(index, this, 'tbody');			   			
			    	});
			    } else {
			    	$("#myTable tr:last").after('<tr><td colspan="8" style="border: 0pt none; text-align: center; padding-top: 20px;">You have no direct reportees.</td></tr>');
			    	$("#groupExec").hide();
			    }
		});
		
		$("#shift1").hide();
		
		// Heirarchy related
		 $("#myTable tr:odd").addClass("odd");
         $("#myTable tr:not(.odd)").hide();
         $("#myTable tr:first-child").show();
		
});
